# ui.py
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QTextEdit, QMessageBox
import paramiko
from sftp_logic import SFTPManager

class RaspberryPiInterface(QWidget):
    def __init__(self):
        super().__init__()

        self.pi_address = "192.168.9.1"
        self.pi_username = ""
        self.pi_password = ""
        self.pi_manager = None  # 添加SFTPManager实例

        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout()

        # 树莓派账号输入框
        self.username_label = QLabel("树莓派账号:")
        self.username_input = QLineEdit(self)
        layout.addWidget(self.username_label)
        layout.addWidget(self.username_input)

        # 树莓派密码输入框
        self.password_label = QLabel("树莓派密码:")
        self.password_input = QLineEdit(self)
        self.password_input.setEchoMode(QLineEdit.Password)
        layout.addWidget(self.password_label)
        layout.addWidget(self.password_input)

        # SFTP连接按钮
        self.connect_button = QPushButton("SSH连接", self)
        self.connect_button.clicked.connect(self.connect_to_pi)
        layout.addWidget(self.connect_button)

        # 命令交互框
        self.command_label = QLabel("命令交互框:")
        self.command_input = QTextEdit(self)
        layout.addWidget(self.command_label)
        layout.addWidget(self.command_input)

        # 命令传输按钮
        self.transfer_button = QPushButton("命令传输", self)
        self.transfer_button.clicked.connect(self.transfer_command)
        layout.addWidget(self.transfer_button)

        # 结果显示框
        self.result_label = QLabel("结果显示框:")
        self.result_display = QTextEdit(self)
        self.result_display.setReadOnly(True)
        layout.addWidget(self.result_label)
        layout.addWidget(self.result_display)

        self.setLayout(layout)
        self.setWindowTitle("树莓派控制界面")

    def connect_to_pi(self):
        # 获取输入的账号和密码
        self.pi_username = self.username_input.text()
        self.pi_password = self.password_input.text()

        # 连接到树莓派
        try:
            self.pi_manager = SFTPManager(self.pi_address, self.pi_username, self.pi_password)
            self.pi_manager.connect()
            QMessageBox.information(self, "Connection Successful", f"Successfully connected to {self.pi_address}",
                                    QMessageBox.Ok)
        except Exception as e:
            error_message = f"Error connecting to {self.pi_address}: {str(e)}"
            print(error_message)
            QMessageBox.critical(self, "Connection Error", error_message, QMessageBox.Ok)

    def transfer_command(self):
        if self.pi_manager is not None:
            # 确保成功连接后再传输命令
            if self.pi_manager.is_connected():
                # 获取输入的命令
                command = self.command_input.toPlainText()

                # 将命令传输到树莓派
                result = self.pi_manager.transfer_command(command)

                # 显示结果
                self.result_display.clear()
                self.result_display.append(result)
            else:
                QMessageBox.warning(self, "Connection Warning", "Please connect to Raspberry Pi first.",
                                    QMessageBox.Ok)

if __name__ == "__main__":
    app = QApplication([])
    window = RaspberryPiInterface()
    window.show()
    app.exec()
